from __future__ import division

def permutate(seq):
    """permutate a sequence and return a list of the permutations"""
    if not seq:
	return [seq]
    else:
	temp = []
	for k in range(len(seq)):
	    part = seq[:k] + seq[k+1:]
	    for m in permutate(part):
		temp.append(seq[k:k+1] + m)
    return temp

def zzip(a, b):
    return (a[0], b[0], a[1], b[1], a[2], b[2], a[3])

patterns = [	
    "((%i %s %i) %s (%i %s %i))",
    "(((%i %s %i) %s %i) %s %i)",
    "(%i %s (%i %s (%i %s %i)))",
]

for pat in patterns:
    ops = "+ - * /".split()
    for operators in [(a, b, c) for a in ops for b in ops for c in ops]:
	for perm in permutate( (1, 5, 6, 7) ):
	    s = pat%(zzip(perm, operators))
	    try:
		if abs(eval(s) - 21) < 0.01 :
		    print s, "=", eval(s)
	    except ZeroDivisionError, e:
		pass
#		print e, s
